package com.fagp.mew.cms.messages;

import com.mew.common.util.StringUtils;
import lombok.Data;
import lombok.ToString;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Data
@ToString
public class PageRequest {
    public int page = 1 ;
    public int limit = 50;
    public String field = "";//排序字段
    public String order = "desc";//默认倒叙
    public String key = "";
    public String value = "";
    public String fromDate;
    public String toDate;


    public void month(String formatStr){
        Date date = new Date();
        SimpleDateFormat format = null;
        if (StringUtils.isNotEmpty(formatStr)){
            format = new SimpleDateFormat(formatStr);
        }else {
            format = new SimpleDateFormat("yyyy-MM-dd");
        }
        //上个月最后一天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getMaximum(Calendar.DAY_OF_MONTH));
        this.fromDate = format.format(calendar.getTime());

        //这个月最后一天
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getMaximum(Calendar.DAY_OF_MONTH));
        this.toDate =  format.format(calendar.getTime());

    }

    public void last30d(String formatStr){
        lastDay(-31,-1,formatStr);
    }

    /**
     *  负 过去 ，0 今日  ，正未来
     * @param startDay
     * @param endDay
     * @param formatStr
     */
    public void lastDay(int startDay,int endDay,String formatStr){
        Date date = new Date();
        SimpleDateFormat format = null;
        if (StringUtils.isNotEmpty(formatStr)){
            format = new SimpleDateFormat(formatStr);
        }else {
            format = new SimpleDateFormat("yyyy-MM-dd");
        }
        Calendar calendar = Calendar.getInstance();
        //fromDate
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+startDay);
        this.fromDate = format.format(calendar.getTime());

        //toDate
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+endDay);
        this.toDate = format.format(calendar.getTime());

    }
    public boolean isDesc(){
        return "desc".equals(this.order);
    }

    public boolean isAsc(){
        return !"desc".equals(this.order);
    }
}
